import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.openstreetmap.osm.Settings;
import org.openstreetmap.osm.data.MemoryDataSet;

import org.openstreetmap.travelingsalesman.gui.MapMover;
import org.openstreetmap.travelingsalesman.gui.widgets.*;

import org.openstreetmap.osm.data.coordinates.LatLon;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.travelingsalesman.navigation.NavigationManager;
import org.openstreetmap.travelingsalesman.painting.SimplePaintVisitor;
import org.openstreetmap.travelingsalesman.painting.SmoothTilePainter;
import org.openstreetmap.travelingsalesman.routing.IRouter;
import org.openstreetmap.travelingsalesman.routing.Route;
import org.openstreetmap.travelingsalesman.routing.Route.RoutingStep;
import org.openstreetmap.travelingsalesman.routing.routers.DepthFirstRouter;
import org.openstreetmap.travelingsalesman.routing.routers.DijkstraRouter;
import org.openstreetmap.travelingsalesman.routing.routers.MultiTargetDijkstraRouter;
import org.openstreetmap.travelingsalesman.routing.routers.TurnRestrictedAStar;
import org.openstreetmap.travelingsalesman.routing.routers.TurnRestrictedMultiTargetDijkstraRouter;
import org.openstreetmap.travelingsalesman.routing.selectors.Motorcar;

public class Routing{

public static void main(String[] args) {
System.out.println("Hello World");
File file=new File("/home/anca/activ_didatica/practica/cluj2.osm");

MemoryDataSet map = (new org.openstreetmap.osm.io.FileLoader(file)).parseOsm();
LatLon startCoord = new LatLon(46.77118,23.58995);
Node startNode = map.getNearestNode(startCoord,null);
System.out.println(startNode.getId());
LatLon targetCoord = new LatLon(46.77396,23.59960 );
Motorcar vehicle=new Motorcar();
Node targetNode = map.getNearestNode(targetCoord,vehicle);
System.out.println(targetNode.getId());

IRouter router = new TurnRestrictedAStar();

Route theRoute = router.route(map, startNode, targetNode,  vehicle);
List<RoutingStep> steps=theRoute.getRoutingSteps();
Iterator<RoutingStep> it=steps.iterator();
while(it.hasNext()){
	   RoutingStep current=it.next();
	System.out.println(current.toString()+" "+current.getWay());
}

NavigationManager nav=new NavigationManager();
nav.setMap(map);


SmoothTilePainter paint=new SmoothTilePainter();



JFrame frame = new JFrame("Routare");
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
});
// This is an empty content area in the frame
JLabel jlbempty = new JLabel("");
jlbempty.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(jlbempty, BorderLayout.CENTER);

MapPanel mapPanel=new MapPanel();
mapPanel.setLayout(new BorderLayout());
mapPanel.setDataSet(map);
final Settings settings = Settings.getInstance();
final double latitude    = settings.getDouble("state.MapPanel.latitude",  46);
final double longitude   = settings.getDouble("state.MapPanel.longitude", 23);
LatLon center = new LatLon(latitude, longitude);
//if (center.isOutSideWorld()) {
  //  center = SimpleMapPanel.DEFAULTCENTER;
//}
//  carefull this is multiplied scale value
mapPanel.setPreferredSize(new Dimension(1600, 1200));
System.out.println(mapPanel.getScale());
double scale  = settings.getDouble("state.MapPanel.scale",10* mapPanel.getScale() * Settings.getProjection().scaleFactor());
if (scale > MapMover.MAXSCALE) {
    scale = MapMover.MAXSCALE;
} else if (scale < MapMover.MINSCALE) {
    scale = MapMover.MINSCALE;
}
System.out.println(scale);

mapPanel.zoomTo(mapPanel.getProjection().latlon2eastNorth(center), scale / mapPanel.getProjection().scaleFactor());
mapPanel.setCurrentRoute(theRoute);

frame.getContentPane().add(mapPanel, BorderLayout.CENTER);



frame.pack();
frame.setVisible(true);
//paint.visitRoute(map,steps);
//assertTrue(theRoute.getSegmentList().size() > 0);

}
}